﻿namespace MvcWebRole4.Models
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using MvcWebRole4.Utility;

    public class MineSweepingRankingNewItem
    {
        public string code { get; set; }

        // name|country|level|score
        public MineSweepingRanking ConvertToEntity()
        {
            if (string.IsNullOrWhiteSpace(this.code))
            {
                return null;
            }

            var decrypted = Crypto.Decrypt(this.code);
            var v = decrypted.Split('|');

            if (v.Length != 4)
            {
                return null;
            }

            try
            {
                var item = new MineSweepingRanking
                    {
                        Name = v[0],
                        Country = v[1],
                        Level = int.Parse(v[2]),
                        Score = int.Parse(v[3]),
                        SubmitDate = DateTime.UtcNow
                    };

                return item;
            }
            catch
            {
                return null;
            }
        }
    }
}